package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;

/**
 * test
 * @author normenhansen
 */
public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    Node shootables;
    Geometry mark;
    
    @Override
    public void simpleInitApp() {
       initCrossHairs();       
       initKeys();
       initMark();
       
       shootables = new Node("Shootables");
       
       rootNode.attachChild(shootables);
       shootables.attachChild(makeCube("a Dragon", -2f, 0f, 1f));
       shootables.attachChild(makeCube("a tin can", -1f, -2f, 1f));
       shootables.attachChild(makeCube("a Sherriff", 0f, 1f, 2f));
       shootables.attachChild(makeCube("a Deputy", 1f, 0f, 4f));
       shootables.attachChild(makeFloor());
    }

    protected void initMark() {
        Sphere sphere = new Sphere(30, 30, 0.2f);
        mark = new Geometry("BOOM!", sphere);
        
        Material mark_mat = new Material(assetManager,
                "Common/MatDefs/Misc/Unshaded.j3md");
        mark_mat.setColor("Color", ColorRGBA.Red);
        mark.setMaterial(mark_mat);
    }
    
    private void initKeys() {
        inputManager.addMapping("Shoot", 
                new KeyTrigger(KeyInput.KEY_SPACE),
                new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(actionListener, "Shoot");
    }
    
    private ActionListener actionListener = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
            if(name.equals("Shoot") && !keyPressed){
                CollisionResults results = new CollisionResults();
                Ray ray = new Ray(cam.getLocation(), cam.getDirection());
                
                shootables.collideWith(ray, results);
                
                System.out.println("------Collisions? " + results.size() + "-----");
                for (int i = 0; i < results.size(); i++) {
                    float dist = results.getCollision(i).getDistance();
                    Vector3f pt = results.getCollision(i).getContactPoint();
                    
                    String hit = results.getCollision(i).getGeometry().getName();
                    
                    System.out.println("* Collision #" + i);
                    System.out.println(" You shot " + hit + " at " + pt + ", " + dist + "wu away.");
                    
                    if (results.size() > 0) {
                        CollisionResult closest = results.getClosestCollision();
                        
                        mark.setLocalTranslation(closest.getContactPoint());
                        rootNode.attachChild(mark);
                    } else {
                        rootNode.detachChild(mark);
                    }
                }
            }
        }
    };
    
    protected Geometry makeCube(String name, float x, float y, float z) {
        Box box = new Box(new Vector3f(x, y, z), 1, 1, 1);
        Geometry cube = new  Geometry(name, box);
        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.randomColor());
        cube.setMaterial(mat1);
        return cube;        
    }
    
    protected Geometry makeFloor() {
        Box box = new Box(new Vector3f(0, -4, -5), 15, .2f, 15);
        Geometry floor = new  Geometry("the Floor", box);
        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.Gray);
        floor.setMaterial(mat1);
        return floor;   
    }
    
    protected void initCrossHairs() {
        guiNode.detachAllChildren();
        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
        BitmapText ch = new BitmapText(guiFont, false);
        
        ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
        ch.setText("+");
        ch.setLocalTranslation(
                settings.getWidth()/2 - guiFont.getCharSet().getRenderedSize()/3*2,
                settings.getHeight()/2 + ch.getLineHeight()/2, 0);
        guiNode.attachChild(ch);
        
    }
}
